import requests
from lxml import etree
class BtcSpider(object):
    def __init__(self):
        self.base_url='https://www.chainnode.com/forum/61-'
        self.headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
        }

    def get_response(self,url):
        response=requests.get(url,headers=self.headers)

        data=response.content.decode('utf-8')
        return data

    def parse_dara(self,data):
        x_data=etree.HTML(data)

        results=x_data.xpath('//span[@data-v-24bb5048]/text()')
        print(len(results))
        #模糊查询
        # result=x_data.xpath('//div[contains(@id,"normolthread")]')
        # print(result)
        for result in results:
            print(result)

    def save_data(self,data):
        with open ('8btc.html','wb') as f:
            f.write(data)

    def run(self):

        #1.拼接 完整url
        url=self.base_url+'1.html'

        data=self.get_response(url)
        # print(data)
        self.parse_dara(data)
        # self.save_data(data)

BtcSpider().run()
